const Router = require('koa-router')
const router = new Router()
const koaBody = require('koa-bodyparser');

const message = require('../model/user_message');

router.prefix('/users');

router.get('/message/:username',async ctx=>{
    const username = ctx.params.username;
    const rows = await message.GETMESSAGE(username);
    //console.log(username);
    ctx.body=rows
})

router.get('/history/:username',async ctx=>{
    const username = ctx.params.username;
    const rows_t = await message.GETHISTORY_total(username);
    const rows_w = await message.GETHISTORY_win(username);
    const game_num = rows_t.length;
    const win_num = rows_w.length;
    const result = game_num?win_num/game_num * 100:"暂无"
    const ratio = result.toFixed(2)+'%'
    ctx.body={total:game_num,win:win_num,ratio:ratio};

})

router.get('/recite/:username',async ctx=>{
    const username = ctx.params.username;
    const recited = await message.GETRECITE(username);
    const firm = await message.GETRECITE_firm(username);
    const min = await message.GETRECITE_min(username);
    const recited_num = recited.length;
    const firm_num = firm.length;
    const min_word = await message.GETDIFFWORD(min[0].word_id);
    const condition_red = (recited_num/5000*100).toFixed(2);
    ctx.body={recited:recited_num,firm:firm_num,min:min_word,condition_recited:condition_red};
})

router.get('/friends/:username',async ctx=>{
    const username = ctx.params.username;
    const friends = await message.GETFRIENDS(username);
    const friends_num = friends.length;
    ctx.body = {friends:friends_num}
})


module.exports = router